﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using US.Entity;
using US.Repository;
using US.Utilities;

namespace US.Business
{
    /// <summary>  
    /// 海关编码
    /// <author>  
    ///     <name>wx</name>  
    ///     <date>2018.11.08 15:39</date>  
    /// </author>  
    /// </summary>  
    public class Bms_Custom_ItemBll : RepositoryFactory<Bms_Custom_Item>
    {
        #region 获取海关编码列表
        /// <summary>
        /// 获取海关编码列表
        /// </summary>
        /// <returns></returns>
        public List<Bms_Custom_Item> GetList()
        {
            return Repository().FindList();
        }

        /// <summary>
        /// 搜索海关编码列表
        /// </summary>
        /// <param name="hsCode">海关编码</param>
        /// <param name="itemName">商品名称</param>
        /// <param name="applyYyyy">适用年份</param>
        /// <returns></returns>
        public List<Bms_Custom_Item> GridListJson(string hsCode, string itemName, string applyYyyy, ref JqGridParam jqgridparam)
        {
            StringBuilder strSql = new StringBuilder();

            //return Repository().FindList(strSql.ToString());

            strSql.Append(@"SELECT bci.BMS_CUSTOM_ITEM_ID
                  ,bci.HS_CODE
                  ,bci.HS_NO
                  ,bci.SUB_CODE
                  ,bci.YYYY
                  ,bci.ITEM_DESC
                  ,bci.REMARK
                  ,bci.GENERAL_DUTY
                  ,bci.PREFERENTIAL_DUTY
                  ,bci.BA
                  ,bci.EXPORT_TAX
                  ,bci.EXCISE
                  ,bci.VALUE_ADDED_TAX
                  ,bci.MONITOR_TERM
                  ,bci.MONITOR_DESC
                  ,bci.IC_TYPE
                  ,hd.Name as MEASURE_CODE1
                  ,hd2.Name as MEASURE_CODE2
                  ,bci.PRICE_REF
                  ,bci.PRICE_REF_H
                  ,hd3.Name as CURRENCY_CODE
                  ,bci.ITEM_DESC_EN
                  ,bci.CREATE_USER_ID
                  ,bci.CREATE_DATE
                  ,bci.LAST_UPDATE_USER_ID
                  ,bci.LAST_UPDATE_DATE
                  ,bci.ELEMENT_DEC
              FROM BMS_CUSTOM_ITEM bci left join 
              Base_HX_DataDictionaryDetail hd on bci.MEASURE_CODE1=hd.code 
              and hd.DataDictionaryId='c86244b3-49b5-47a6-b1a8-69254821b4c6'
              left join 
              Base_HX_DataDictionaryDetail hd2 on bci.MEASURE_CODE2=hd2.code 
              and hd2.DataDictionaryId='c86244b3-49b5-47a6-b1a8-69254821b4c6'
              left join 
              Base_HX_DataDictionaryDetail hd3 on bci.CURRENCY_CODE=hd3.code 
              and hd3.DataDictionaryId='23e1c40b-1628-4f6e-a312-bf116dc51821' 
                where 1=1 ");

            if (!string.IsNullOrEmpty(hsCode))
            {
                strSql.Append(@" and HS_CODE like '%");
                strSql.Append(hsCode.Trim());
                strSql.Append(@"%'");
            }
            if (!string.IsNullOrEmpty(itemName))
            {
                strSql.Append(@" and ITEM_DESC like '%");
                strSql.Append(itemName.Trim());
                strSql.Append(@"%'");
            }
            if (!string.IsNullOrEmpty(applyYyyy))
            {
                strSql.Append(@" and YYYY like '%");
                strSql.Append(applyYyyy.Trim());
                strSql.Append(@"%'");
            }

            List<Bms_Custom_Item> ListData = Repository().FindListPageBySql(strSql.ToString(), ref jqgridparam);
            return ListData;


        }


        /// <summary>
        /// 获取已有适用年限列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetYYYYList()
        {
            string strSql = @"select YYYY from Bms_Custom_Item
                        group by YYYY
                        order by YYYY asc";

            return Repository().FindTableBySql(strSql);
        }


        /// <summary>
        /// 复制海关编码
        /// </summary>
        /// <param name="yyyyOld">实体对象</param>
        /// <param name="yyyyNew">主键值</param>
        /// <returns></returns>
        public void SubmitCopy(int yyyyOld, int yyyyNew)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"insert into BMS_CUSTOM_ITEM
                SELECT NEWID() ,HS_CODE ,HS_NO ,SUB_CODE, ");
            strSql.Append(yyyyNew);
            strSql.Append(@", ITEM_DESC ,REMARK ,GENERAL_DUTY ,PREFERENTIAL_DUTY ,BA
               ,EXPORT_TAX ,EXCISE ,VALUE_ADDED_TAX ,MONITOR_TERM ,MONITOR_DESC ,IC_TYPE
               ,MEASURE_CODE1 ,MEASURE_CODE2 ,PRICE_REF ,PRICE_REF_H ,CURRENCY_CODE ,ITEM_DESC_EN, '");
            strSql.Append(ManageProvider.Provider.Current().UserId);
            strSql.Append("', GETDATE(), '");
            strSql.Append(ManageProvider.Provider.Current().UserId);
            strSql.Append("', GETDATE(), ELEMENT_DEC FROM BMS_CUSTOM_ITEM where YYYY=");
            strSql.Append(yyyyOld);
            Repository().ExecuteBySql(strSql);

        }

        #endregion


    }
}
